package com.pearlsoft.webapp.rms.vmmgr;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;

import com.pearlsoft.rms.VcloudManager;
import com.pearlsoft.webapp.core.annotation.QueryHandlerComponent;
import com.pearlsoft.webapp.core.dataset.DataSet;
import com.pearlsoft.webapp.core.dataset.Query;
import com.pearlsoft.webapp.core.dataset.Record;
import com.pearlsoft.webapp.core.dataset.SimpleRecord;
import com.pearlsoft.webapp.core.web.basehandler.AbsBaseQueryHandler;
import com.vmware.vcloud.sdk.Organization;
import com.vmware.vcloud.sdk.VCloudException;
import com.vmware.vcloud.sdk.VM;
import com.vmware.vcloud.sdk.Vapp;
import com.vmware.vcloud.sdk.Vdc;
import com.vmware.vcloud.sdk.VirtualDisk;

@QueryHandlerComponent(name = "VMMgrQueryHandler")
public class VMMgrQueryHandler extends AbsBaseQueryHandler {


	/*
	 * 从vm中获取数据并转换成record
	 */
	private Record changeVmToRecord(VM vm,String vApp_Name,String vDC_Name) {
		SimpleRecord recrod = new SimpleRecord();
		try {
			recrod.setValue("VApp_Name", vApp_Name);
			recrod.setValue("VDC_Name", vDC_Name);
			recrod.setValue("VM_ID", vm.getResource().getId());
			recrod.setValue("VM_Name", vm.getResource().getName());
			recrod.setValue("VM_Status", vm.getVMStatus());
			recrod.setValue("VM_Cpu", vm.getCpu().getNoOfCpus());
			recrod
					.setValue("VM_Memory", vm.getMemory().getMemorySize()
							+ " Mb");

			BigInteger diskSize = new BigInteger("0");

			for (VirtualDisk disk : vm.getDisks()) {
				if (disk.isHardDisk()) {

					diskSize = diskSize.add(disk.getHardDiskSize());

				}
			}

			recrod.setValue("VM_HardDisk", diskSize.toString() + " Mb");
			recrod.setValue("VM_Netcards", vm.getNetworkCards().size());
		} catch (VCloudException e) {
			e.printStackTrace();
		}
		return recrod;

	}

	/*
	 * 获取虚拟机列表
	 */
	public void handler(Query query, DataSet ds) throws Exception {
		
		String vApp_Name = (String)query.get("VApp_Name");
		String vDC_Name = (String)query.get("VDC_Name");
		VcloudManager vcm = this.getBean("vcloudManager",
				VcloudManager.class);
		//vcm.login("https://192.168.0.7", "administrator@test", "abc123..");
		Map<String, Organization> orgs = vcm.getOrgMap();
		Organization org = orgs.get(System.getProperty("ORG_NAME"));
		Map<String, Vdc> vdcs = vcm.getVdcMap(org);
		List<Record> list = new ArrayList<Record>();
		if(vApp_Name==null||vDC_Name==null||vApp_Name.isEmpty()||vDC_Name.isEmpty())
		{
			for (Map.Entry<String, Vdc> entry : vdcs.entrySet()) {
				Vdc vdc = entry.getValue();
				Map<String, Vapp> vapps = vcm.getVappMap(vdc);
				for(Map.Entry<String, Vapp> vappEntry:vapps.entrySet()  )
				{
					Vapp vapp = vappEntry.getValue();
					List<VM> vms = vcm.getVMList(vapp);
					for (VM vm : vms) {
						list.add(changeVmToRecord(vm,vapp.getResource().getName(),vdc.getResource().getName()));
					}
				}					
			}
			
		}else{
			Vdc vdc = vdcs.get(vDC_Name);
			Map<String, Vapp> vapps = vcm.getVappMap(vdc);
			Vapp vapp = vapps.get(vApp_Name);
			List<VM> vms = vcm.getVMList(vapp);
			for (VM vm : vms) {
				list.add(changeVmToRecord(vm,vapp.getResource().getName(),vdc.getResource().getName()));
			}
		}
		
		ds.setRecords(list);

		// if(query.getPageSize() > 0 ){
		//			
		// PageResult pageResult =
		// roleServer.getBaseSimpleJdbcDao().findByPage(sql, query.getStart(),
		// query.getPageSize(), params, generalRowMapper);
		// ds.setPageSize( query.getPageSize());
		// ds.setTotalCount(pageResult.getTotalCount());
		// ds.setRecords(pageResult.getRecords());
		// }else{
		// List<Record> list = roleServer.getBaseSimpleJdbcDao().query(sql,
		// params, generalRowMapper);
		// ds.setRecords(list);
		// }

	}

	@Override
	public void advSearch(Query query, DataSet ds) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void getEntity(Query query, DataSet ds) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void quickSearch(Query query, DataSet ds) throws Exception {
		// TODO Auto-generated method stub
		String searchValue = (String) query.get("searchValue");
		//写死,以后更改
		String vDC_Name = "test";//(String)ds.getConditions().get("VDC_Name");
		String vApp_Name = "vApp_administrator_1";//(String)ds.getConditions().get("VApp_Name");
		VcloudManager vcm = this.getBean("vcloudManager",
				VcloudManager.class);
		//vcm.login("https://192.168.0.7", "administrator@test", "abc123..");
		vcm.setLogLevel(Level.OFF);
		Map<String, Organization> orgs = vcm.getOrgMap();
		Organization org = orgs.get(System.getProperty("ORG_NAME"));
		Map<String, Vdc> vdcs = vcm.getVdcMap(org);
		Vdc vdc = vdcs.get(vDC_Name);
		Map<String, Vapp> vapps = vcm.getVappMap(vdc);
		Vapp vapp = vapps.get(vApp_Name);
		List<VM> vms = vcm.getVMList(vapp);
		List<Record> list = new ArrayList<Record>();
		for (VM vm : vms) {
			if (vm.getResource().getName().indexOf(searchValue) != -1)
				list.add(changeVmToRecord(vm,vapp.getResource().getName(),vdc.getResource().getName()));
		}
		ds.setRecords(list);
	}

}
